package com.chinatechstar.component.commons.utils;

import com.chinatechstar.admin.entity.SysOrg;
import com.chinatechstar.admin.entity.SysUser;
import com.chinatechstar.admin.mapper.SysOrgMapper;
import com.chinatechstar.admin.mapper.SysUserMapper;
import com.chinatechstar.base.entity.SysArea;
import com.chinatechstar.base.entity.SysProdOrg;
import com.chinatechstar.base.entity.SysWell;
import com.chinatechstar.base.mapper.SysAreaMapper;
import com.chinatechstar.base.mapper.SysProdOrgMapper;
import com.chinatechstar.base.mapper.SysWellMapper;
import com.chinatechstar.component.commons.exception.AjaxExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 基础数据常用验证工具类
 *
 * @author Rei
 * @date 2021/4/21
 */
@Component
public class ValidateUtils {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;
    @Autowired
    private SysProdOrgMapper sysProdOrgMapper;
    @Autowired
    private SysAreaMapper sysAreaMapper;
    @Autowired
    private SysWellMapper sysWellMapper;
    private static ValidateUtils validateUtils;

    private ValidateUtils() {
    }

    @PostConstruct
    public void init() {
        validateUtils = this;
    }

    /**
     * 验证用户
     */
    public static SysUser validateUsername(String username) {
        if (StrUtils.isNullOrEmpty(username)) {
            throw new IllegalArgumentException(AjaxExceptionEnum.USER_ID_IS_REQUIRED.getMsg());
        }
        SysUser sysUser = validateUtils.sysUserMapper.getUserByUsername(username);
        if (sysUser == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.USER_NOT_FIND.getMsg());
        }
        return sysUser;
    }

    /**
     * 验证组织机构
     */
    public static SysOrg validateOrg(Long orgId) {
        if (orgId == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.ORG_ID_IS_REQUIRED.getMsg());
        }
        SysOrg sysOrg = validateUtils.sysOrgMapper.findSysOrgById(orgId);
        if (sysOrg == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.ORG_NOT_FIND.getMsg());
        }
        return sysOrg;
    }

    /**
     * 验证生产单位
     */
    public static SysProdOrg validateProdOrg(Long orgId) {
        if (orgId == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.PROD_ORG_ID_IS_REQUIRED.getMsg());
        }
        SysProdOrg prodOrg = validateUtils.sysProdOrgMapper.findSysProdOrgById(orgId);
        if (prodOrg == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.PROD_ORG_NOT_FIND.getMsg());
        }
        return prodOrg;
    }

    /**
     * 验证生产单位名称是否重复
     */
    public static void validateOrgName(String orgName) {
        if (StrUtils.isNullOrEmpty(orgName)) {
            throw new IllegalArgumentException(AjaxExceptionEnum.PROD_ORG_NAME_IS_REQUIRED.getMsg());
        }
        SysProdOrg orgByName = validateUtils.sysProdOrgMapper.findSysProdOrgByName(orgName);
        if (orgByName != null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.PROD_ORG_ALREADY_EXISTS.getMsg());
        }
    }

    /**
     * 验证生产单位ID是否重复
     */
    public static void validateOrgId(String orgId) {
        if (StrUtils.isNullOrEmpty(orgId)) {
            throw new IllegalArgumentException(AjaxExceptionEnum.PROD_ORG_ID_IS_REQUIRED.getMsg());
        }
        SysProdOrg orgByOrgId = validateUtils.sysProdOrgMapper.findSysProdOrgByOrgId(orgId);
        if (orgByOrgId != null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.PROD_ORG_ALREADY_EXISTS.getMsg());
        }
    }

    /**
     * 验证区块
     */
    public static SysArea validateArea(Long areaId) {
        if (areaId == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.AREA_ID_IS_REQUIRED.getMsg());
        }
        SysArea sysArea = validateUtils.sysAreaMapper.findSysAreaById(areaId);
        if (sysArea == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.AREA_NOT_FIND.getMsg());
        }
        return sysArea;
    }

    /**
     * 验证区块父级
     */
    public static void validateParentArea(String areaId) {
        if (StrUtils.isNullOrEmpty(areaId)) {
            throw new IllegalArgumentException(AjaxExceptionEnum.AREA_PARENT_ID_IS_REQUIRED.getMsg());
        }
        SysArea sysArea = validateUtils.sysAreaMapper.findSysAreaByAreaId(areaId);
        if (sysArea == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.AREA_PARENT_NOT_FIND.getMsg());
        }
    }

    /**
     * 验证区块名称是否重复
     */
    public static void validateAreaName(String areaName) {
        if (StrUtils.isNullOrEmpty(areaName)) {
            throw new IllegalArgumentException(AjaxExceptionEnum.AREA_NAME_IS_REQUIRED.getMsg());
        }
        SysArea areaByAreaName = validateUtils.sysAreaMapper.findSysAreaByAreaName(areaName);
        if (areaByAreaName != null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.AREA_ALREADY_EXISTS.getMsg());
        }
    }

    /**
     * 验证区块ID是否重复
     */
    public static void validateAreaId(String areaId) {
        if (StrUtils.isNullOrEmpty(areaId)) {
            throw new IllegalArgumentException(AjaxExceptionEnum.AREA_ID_IS_REQUIRED.getMsg());
        }
        SysArea areaByAreaId = validateUtils.sysAreaMapper.findSysAreaByAreaId(areaId);
        if (areaByAreaId != null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.AREA_ALREADY_EXISTS.getMsg());
        }
    }

    /**
     * 验证井
     */
    public static SysWell validateWell(Long wellId) {
        if (wellId == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.WELL_ID_IS_REQUIRED.getMsg());
        }
        SysWell sysWell = validateUtils.sysWellMapper.findSysWellById(wellId);
        if (sysWell == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.WELL_NOT_FIND.getMsg());
        }
        return sysWell;
    }

    /**
     * 验证井（A2 ID）
     */
    public static SysWell validateWell(String wellId) {
        if (wellId == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.A2_WELL_ID_IS_REQUIRED.getMsg());
        }
        SysWell sysWell = validateUtils.sysWellMapper.findSysWellByWellId(wellId);
        if (sysWell == null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.A2_WELL_NOT_FIND.getMsg());
        }
        return sysWell;
    }

    /**
     * 验证井名称是否重复
     */
    public static void validateWellName(String wellName) {
        if (StrUtils.isNullOrEmpty(wellName)) {
            throw new IllegalArgumentException(AjaxExceptionEnum.A2_WELL_NAME_IS_REQUIRED.getMsg());
        }
        SysWell wellByWellCommonName = validateUtils.sysWellMapper.findSysWellByWellCommonName(wellName);
        if (wellByWellCommonName != null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.WELL_ALREADY_EXISTS.getMsg());
        }
    }

    /**
     * 验证井ID是否重复
     */
    public static void validateWellId(String wellId) {
        if (StrUtils.isNullOrEmpty(wellId)) {
            throw new IllegalArgumentException(AjaxExceptionEnum.A2_WELL_ID_IS_REQUIRED.getMsg());
        }
        SysWell wellByWellId = validateUtils.sysWellMapper.findSysWellByWellId(wellId);
        if (wellByWellId != null) {
            throw new IllegalArgumentException(AjaxExceptionEnum.WELL_ALREADY_EXISTS.getMsg());
        }
    }

}
